package cn.net.cfss.fgbp.provider.console.spi;

import cn.net.cfss.fgbp.api.console.dto.SysAppMenuDto;
import cn.net.cfss.fgbp.api.console.inte.ISysAppMenuService;
import cn.net.cfss.fgbp.api.console.vo.SysAppMenuVo;
import cn.net.cfss.fgbp.provider.console.biz.SysAppMenuDtoBiz;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author oygq
 * @title: ISysAppMenuServiceImpl
 * @description: TODO
 * @date 2019/4/8 14:58
 */
@RestController
@Slf4j
public class SysAppMenuSpi implements ISysAppMenuService {

    @Autowired
    SysAppMenuDtoBiz appMenuDtoBiz;

    @Override
    public boolean insert(SysAppMenuVo vo) {
        try {
            //appMenuDtoBiz.insertBatch(vo);
            SysAppMenuDto dto = new SysAppMenuDto();
            dto.setAppId(vo.getAppId());
            appMenuDtoBiz.delete(dto);
            appMenuDtoBiz.insertBatch(vo);
        } catch (Exception e) {
            log.error("应用菜单新增错误:{}", e.getMessage());
            return false;
        }
        log.info("请求参数:{}，返回参数：{}", vo, true);
        return true;
    }

    @Override
    public boolean update(SysAppMenuVo vo) {
        try {
            //先删除旧的权限，在新增新的权限
            SysAppMenuDto dto = new SysAppMenuDto();
            dto.setAppId(vo.getAppId());
            appMenuDtoBiz.delete(dto);
            appMenuDtoBiz.insertBatch(vo);
        } catch (Exception e) {
            log.error("应用菜单修改错误:{}", e.getMessage());
            return false;
        }
        log.info("请求参数:{}，返回参数：{}", vo, true);
        return true;
    }

    @Override
    public boolean delete(String appMenuId) {
        try {
            appMenuDtoBiz.deleteById(appMenuId);
        } catch (Exception e) {
            log.error("应用菜单删除错误:{}", e.getMessage());
            return false;
        }
        log.info("请求参数:{}，返回参数：{}", appMenuId, true);
        return true;
    }

    @Override
    public boolean deleteBatch(String[] appMenuIds) {
        try {
            appMenuDtoBiz.deleteBatch(appMenuIds);
        } catch (Exception e) {
            log.error("应用菜单批量删除错误:{}", e.getMessage());
            return false;
        }
        log.info("请求参数:{}，返回参数：{}",  appMenuIds, true);
        return true;
    }

    @Override
    public List<String> queryByAppId(String appId) {
        SysAppMenuVo vo = new SysAppMenuVo();
        vo.setAppId(Long.valueOf(appId));
        List<SysAppMenuDto> appMenus = appMenuDtoBiz.selectList(vo);
        List<String> menus = new ArrayList<>();
        for(SysAppMenuDto dto : appMenus){
            menus.add(dto.getMenuId()+ "");
        }
        log.info("请求参数:{}，返回参数：{}", appId, menus);
        return menus;
    }

    @Override
    public boolean deleteByMenus(SysAppMenuVo vo) {
        try {
            appMenuDtoBiz.deleteByMenus(vo);
        } catch (Exception e) {
            log.error("应用菜单批量删除错误:{}", e.getMessage());
            return false;
        }
        log.info("请求参数:{}，返回参数：{}", vo, true);
        return true;
    }
}
